package org.opens.guava.utilites.eventBus.events;

import com.google.common.eventbus.EventBus;
import org.junit.Test;
import org.opens.guava.utilites.eventBus.listeners.SimpleListener;

public class UnRegisterExample {

    /**
     * 功能:
     *      测试eventBus.unregister(listener);方法将当前监听器从事件总线中移除.
     * 输出:
     *      13:39:37.031 [main] INFO org.opens.guava.utilites.eventBus.listeners.SimpleListener - 发生的 event: [simple event]
     *      13:39:37.031 [main] INFO org.opens.guava.utilites.eventBus.listeners.SimpleListener - 发生的 event: [simple event]
     * 说明:
     *      1. 可以发现, 在执行反注册这个监听器之后, 当前监听器就没有作用了.
     */
    @Test
    public void test2() {
        SimpleListener listener = new SimpleListener();
        final EventBus eventBus = new EventBus();
        eventBus.register(listener);
        eventBus.post("simple event");
        eventBus.post("simple event");

        eventBus.unregister(listener);
        eventBus.post("simple event");
        eventBus.post("simple event");
        eventBus.post("simple event");
        eventBus.post("simple event");
    }

}
